package org.authsum.stitches.wicket.contentversion.detail;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.PropertyModel;
import org.authsum.stitches.external.*;
import org.authsum.stitches.wicket.BasePanel;
import org.authsum.stitches.wicket.DetailPanel;
import org.authsum.stitches.wicket.attribute.AttributesPanel;
import org.authsum.stitches.wicket.attribute.detail.AttributePanel;
import org.authsum.stitches.wicket.contentdef.ContentDefsPanel;
import  org.authsum.stitches.wicket.contentdef.detail.*;
import org.authsum.stitches.wicket.content.*;
import org.authsum.stitches.wicket.contentdef.ContentDefSelectionListener;
import org.authsum.stitches.wicket.contentversion.*;


public class ContentVersionPanel extends DetailPanel  implements ContentSelectionListener {
	

	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	Label contentId  = null;

	private static Log logger = LogFactory.getLog(ContentDefPanel.class);

	private ContentVersionHolder contentVersionHolder = null;
	
	public ContentVersionPanel(String id,ContentVersionHolder contentVersionHolder,ContentVersionsPanel contentVersionsPanel) {
		super(id);

		this.contentVersionHolder = contentVersionHolder;


		form = new Form("detailForm", new CompoundPropertyModel(contentVersionHolder));
		add(form);
		addSaveButtons();
		form.add(newRequiredTextField("name", 255));


		nameModel= new PropertyModel(contentVersionHolder,"name");
		nameLabel = new Label("nameLabel",nameModel);
		add(nameLabel);
		


		
		PropertyModel pm = new PropertyModel(contentVersionHolder,"id");
		Label objectId = new Label("objectId", new PropertyModel(contentVersionHolder,"id"));
		add(objectId);
		
		PropertyModel pm2 = new PropertyModel(contentVersionHolder,"contentDefId");
		contentId = new Label("contentId", new PropertyModel(contentVersionHolder,"contentId"));
		add(contentId);
		contentId.setOutputMarkupId(true);
		
		
		
		
		
		//AttributesPanel attributesPanel  = new AttributesPanel("attributesPanel",contentDefHolder,contentDefsPanel);
		//add(attributesPanel);
		
		
		
		contentModal = new ModalWindow("contentModal");
		add(contentModal);
		//String id, final RoadTrip roadTrip, final RoadTripDay roadTripDay, RoadtripDetailPanel rtdp,DmiPanel dmiPanel
		contentModal.setContent(	new ContentsPanel(contentModal.getContentId(), this));
		contentModal
				.setTitle("Select a content for you your content version");
		//daysModal.setCookieName("daysModal");

		contentModal
				.setCloseButtonCallback(new ModalWindow.CloseButtonCallback() {
					private static final long serialVersionUID = 1L;

					public boolean onCloseButtonClicked(
							AjaxRequestTarget target) {
						logger.debug("attributeModal, onCloseButtonClicked");
						return true;
					}
				});

		contentModal
				.setWindowClosedCallback(new ModalWindow.WindowClosedCallback() {
					private static final long serialVersionUID = 1L;

					public void onClose(AjaxRequestTarget target) {
						logger.debug("attributeModal, setWindowClosedCallback");
					}
				});
				

		
		
		AjaxLink selectLink = (new AjaxLink("selectLink") {

			@Override
			public void onClick(AjaxRequestTarget target) {
				contentModal.show(target);

			}

		});
		add(selectLink);
		
		postInit();
		
		
		
	}

	ModalWindow contentModal = new ModalWindow("contentModal");

	@Override
	protected void saveDetailObject(AjaxRequestTarget target) {
		//need to save the contentDefHolder
		ContentVersionRequest contentVersionRequest = new ContentVersionRequest();
		contentVersionRequest.setContentVersionHolder(contentVersionHolder);


		

		ContentVersionResponse contentVersionResponse = getStitchesService().saveContentVersion(contentVersionRequest);
		//contentDefResponse.getContentDefHolders().getContentDefHolder().
		if (contentVersionResponse.getContentVersionHolders().getContentVersionHolder() == null) {
			System.out.println("getContentHolder is nul");
		} else {
			this.contentVersionHolder = contentVersionResponse.getContentVersionHolders().getContentVersionHolder().get(0);
		}

		
		
		//let's refresh the screen
		info("Content Version Saved");
		target.addComponent(this);
		
		
		
	}



	@Override
	protected List<String> getValidity() {
		List<String> list = new ArrayList<String>();
		return list;
	}



	public void contentSelected(AjaxRequestTarget target, ContentHolder cdh) {
		contentVersionHolder.setContentId(cdh.getId());
		contentModal.close(target);
		target.addComponent(contentId);
		
		
	}





	
	
}
